package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.MineOrderDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderDeleteBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderExtendTransportBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by ZH on 2018/3/27.
 * 订单详情页 Model
 */

public class OrderDataModel implements OrderDataModelImpl{

    private OnOrderDataResponseListener mListener;
    private OnOrderExtentListener mExtentListener;
    private OnOrderDeleteListener mDeleteListener;

    private static final String TAG = "OrderDataModel";

    public OrderDataModel(OnOrderDataResponseListener listener,OnOrderExtentListener extentListener,
                          OnOrderDeleteListener deleteListener) {
        mListener = listener;
        mExtentListener=extentListener;
        mDeleteListener=deleteListener;
    }

    public interface OnOrderDataResponseListener{
        void onGetDetailSuccess(MineOrderDataBean bean);
        void onGetDetailError(Throwable throwable);//获取细节
    }
    public interface OnOrderExtentListener{
        void onExtentSuccess(OrderExtendTransportBean bean);
        void onExtentError(Throwable throwable);
    }
    public interface OnOrderDeleteListener{
        void onGetDeleteSuccess(OrderDeleteBean bean);
        void onGetDeleteError(Throwable throwable);
    }

    @Override
    public void getOrderDetail(String oid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("orders_id", oid)
                .build();
        Observable<MineOrderDataBean> observable = DataManager.getOrderData(body);
        Observable<MineOrderDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<MineOrderDataBean>() {
            @Override
            public void onNext(MineOrderDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }

            @Override
            public void onComplete() {
                //完成
            }
        });
    }

    @Override
    public void getOrderExtentTransport(String oid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("ordersid", oid)
                .build();
        Observable<OrderExtendTransportBean> observable = DataManager.extentOrderRequest(body);
        Observable<OrderExtendTransportBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderExtendTransportBean>() {
            @Override
            public void onNext(OrderExtendTransportBean bean) {
                if (mExtentListener != null) {
                    LogUtils.e("网络正确", TAG);
                    mExtentListener.onExtentSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mExtentListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    LogUtils.e("网络出错", TAG);
                    mExtentListener.onExtentError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getOrderDelete(String oid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("ordersid", oid)
                .build();
        Observable<OrderDeleteBean> observable = DataManager.delOrderRequest(body);
        Observable<OrderDeleteBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderDeleteBean>() {
            @Override
            public void onNext(OrderDeleteBean bean) {
                if (mDeleteListener != null) {
                    mDeleteListener.onGetDeleteSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mDeleteListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mDeleteListener.onGetDeleteError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
